﻿using DataAchive.Base;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace DataAchive.Model.ViewModels
{
    public class NgViewModel : ViewModelBase
    {
        public NgViewModel()
        {
            //        ItemsData = new ObservableCollection<MultiCbxBaseData>
            //{
            //    new MultiCbxBaseData { ViewName = "Item 1", IsCheck = false },
            //    new MultiCbxBaseData { ViewName = "Item 2", IsCheck = true },
            //    new MultiCbxBaseData { ViewName = "Item 3", IsCheck = false }
            //};
            SetComboxItems();


        }


        private ObservableCollection<MultiCbxBaseData> _selectedItems;
        public ObservableCollection<MultiCbxBaseData> SelectedItems
        {
            get => _selectedItems;
            set
            {
                _selectedItems = value;
                OnPropertyChanged();
            }
        }

        #region SiteInfoWindow
        ///// <summary>
        ///// 查询数据
        ///// </summary>
        //ObservableCollection<NgViewModel> _allData = new ObservableCollection<NgViewModel>();

        //public ObservableCollection<NgViewModel> AllData
        //{

        //    get { return _allData; }
        //    set
        //    {
        //        _allData = value;
        //        OnPropertyChanged();
        //    }
        //}



        Page<ObservableCollection<NgData>> _ngInfoData = new Page<ObservableCollection<NgData>>();

        public Page<ObservableCollection<NgData>> NgInfoData
        {

            get { return _ngInfoData; }
            set
            {
                _ngInfoData = value;
                OnPropertyChanged();
            }
        }


        private NgData _ngQuery = new NgData();
        public NgData NgQuery
        {
            get { return _ngQuery; }
            set
            {
                _ngQuery = value;
                OnPropertyChanged();
            }
        }





        ObservableCollection<MultiCbxBaseData> _ItemsData = new ObservableCollection<MultiCbxBaseData>();
        public ObservableCollection<MultiCbxBaseData> ItemsData
        {

            get { return _ItemsData; }
            set
            {
                _ItemsData = value;
                OnPropertyChanged();
            }
        }



        private void SetComboxItems()
        {
            ObservableCollection<MultiCbxBaseData> items = new ObservableCollection<MultiCbxBaseData>();
            PropertyInfo[] propertyInfos = typeof(NgData).GetProperties();
            if (propertyInfos.Length > 0)
            {
                MultiCbxBaseData selectAll = new MultiCbxBaseData() { ID = -1, ViewName = "全选" };
                selectAll.ISCheckChanged += SelectAll_Checked;
                items.Add(selectAll);
                foreach (var propertyInfo in propertyInfos)
                {
                    DescriptionAttribute descriptionAttribute = propertyInfo.GetCustomAttribute<DescriptionAttribute>();
                    string name = descriptionAttribute == null ? propertyInfo.Name : descriptionAttribute.Description;

                    // 正则表达式，匹配中文字符
                    const string chinesePattern = "[\u4e00-\u9fa5]";
                    // 正则表达式，匹配英文字符
                    const string englishPattern = "[a-zA-Z]";

                    bool hasChinese = Regex.IsMatch(name, chinesePattern);

                    bool hasEnglish = Regex.IsMatch(name, englishPattern);
                    //bool isAllChinese = name.All(c => c >= 0x4E00 && c <= 0x9FA5);
                    if (hasChinese && hasEnglish)
                    {
                        items.Add(new MultiCbxBaseData() { ViewName = name, IsCheck = true });
                    }
                    else if(hasChinese)
                    {
                        items.Add(new MultiCbxBaseData() { ViewName = name, IsCheck = true });
                    }
                    else if (hasEnglish)
                    {
                        items.Add(new MultiCbxBaseData() { ViewName = name, IsCheck = false });
                    }

                }
                ItemsData = items;
            }
        }

        private void SelectAll_Checked(object sender, EventArgs e)
        {
            var list = ItemsData?.ToList();
            if (list != null && list.Any())
            {
                MultiCbxBaseData allCheck = (MultiCbxBaseData)sender;
                foreach (var item in list)
                {
                    if (item.ID == -1)
                        continue;
                    item.IsCheck = allCheck.IsCheck;
                }
            }
        }

        #endregion

    }
    public class SiteData
    {
        [Description("线别号")]
        public string Line_Id { get; set; }
        [Description("设备号")]
        public string Equipment_Id { get; set; }
        [Description("IP")]
        public string IP { get; set; }
        [Description("Row_Number")]
        public string Row_Number { get; set; }
        [Description("Row_Type")]
        public string Row_Type { get; set; }
        [Description("Row_Order")]
        public string Row_Order { get; set; }
        [Description("Column_Number")]
        public string Column_Number { get; set; }
        [Description("Column_Type")]
        public string Column_Type { get; set; }
        [Description("Column_Order")]
        public string Column_Order { get; set; }
        [Description("图片的上下间距")]
        public string Top_Bottom_Margin { get; set; }
        [Description("图片的左右间距")]
        public string Left_Right_Margin { get; set; }
        [Description("换班开始时间")]
        public string Shift_Start_Time { get; set; }
        [Description("班次")]
        public string Shifting { get; set; }
        [Description("Currentshift")]
        public string Currentshift { get; set; }
    }

    public class NgData : SiteData
    {
        [Description("ID")]
        public int? ID { get; set; }
        [Description("条码")]
        public string Lot_Id { get; set; }
        [Description("Info")]
        public string Info { get; set; }
        [Description("图片")]
        public string Image { get; set; }
        [Description("是否检查")]
        public string Checked { get; set; }
        [Description("日期")]
        public string Date { get; set; }
        [Description("缺陷类型")]
        public string Type { get; set; }
        [Description("AI_Info")]
        public string AI_Info { get; set; }
        [Description("机台ID")]
        public int? Site_Id { get; set; }
        [Description("完成时间")]
        public string Finish_Time { get; set; }
        [Description("AI_Result")]
        public string AI_Result { get; set; }
        [Description("Manua_Result")]
        public string Manua_Result { get; set; }
        [Description("Mes_Check")]
        public int? Mes_Check { get; set; }

    }
}
